// Shoot To Kill.cpp : Defines the entry point for the console application.
//

#ifdef _WIN32
#include <Windows.h>
#endif

#include <gl/GL.h>
#include <gl/GLU.h>
#include <SDL.h>
#include <SDL_main.h>
#include <SDL_opengl.h>
#include <cmath>
#include "constants.h"
#include "Camera.h"
#include "image.h"
#include "OGLhelpers.h"
#include "Line.h"

#define MAP_WIDTH 35
#define MAP_HEIGHT 32

Uint8 *keys;
int worldMap[MAP_HEIGHT][MAP_WIDTH] =
{
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,3,0,3,0,3,0,0,0,1},//
  {1,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,1},
  {1,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,1},
  {1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

int main(int argc, char* argv[])
{
	SDL_Surface *scr;
	bool live = true;
	SDL_Event e;
	Camera camera;

	if (SDL_Init(SDL_INIT_VIDEO) != 0)
		return 1;

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	scr = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 32, SDL_OPENGL);
	glEnable(GL_TEXTURE_2D);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, (GLfloat) SCR_WIDTH / (GLfloat) SCR_HEIGHT, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glDisable(GL_LIGHTING);

	GLuint gun, wall;
	loadImage("C:\\Users\\Vincent\\Pictures\\pistol.png", &gun);
	loadImage("C:\\Users\\Vincent\\Pictures\\Textures\\Stone Wall.png", &wall);

	createDispLists();

	camera.moveTo(2.0f, 0.0f, 2.0f);

	while (live)
	{
		static Uint32 lastTime = SDL_GetTicks();
		Uint32 deltaTime;

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();

		SDL_PollEvent(&e);

		if (e.type == SDL_QUIT)
			live = false;

		keys = SDL_GetKeyState(NULL);

		if (keys[SDLK_w])
		{
			float dirX = -cos(camera.yrot);
			float dirZ = sin(camera.yrot);
			int camX = (int) camera.x / 2;
			int camZ = (int) camera.z / 2;
			int calcX = abs((int) (camera.x  + 2.0f + dirX * Camera::moveSpeed + 1.0f)) / 2, 
				calcZ = abs((int) (camera.z + 2.0f + dirZ * Camera::moveSpeed)) / 2;

			if ((camX > 1 && camX < MAP_HEIGHT - 1) && (camZ > 2 && camZ < MAP_WIDTH - 2))
			{
				if (worldMap[calcX][(int) camZ] == 0)
					camera.moveX(1);
				if (worldMap[(int) camX][calcZ] == 0)
					camera.moveZ(1);
			}
			
		}

		if (keys[SDLK_s])
		{
			camera.moveForward(-1);
		}

		if (keys[SDLK_a])
		{
			/*double oldDirX = camera.dirX;
      		camera.dirX = camera.dirX * cos(Camera::rotSpeed) - camera.dirY * sin(Camera::rotSpeed);
      		camera.dirY = oldDirX * sin(camera.rotSpeed) + camera.dirY * cos(Camera::rotSpeed);
   	 	   	double oldPlaneX = camera.planeX;*/

			camera.yrot -= Camera::rotSpeed;
		}

		if (keys[SDLK_d])
		{
			/*double oldDirX = camera.dirX;
      		camera.dirX = camera.dirX * cos(-Camera::rotSpeed) - camera.dirY * sin(-Camera::rotSpeed);
      		camera.dirY = (float) oldDirX * sin(-Camera::rotSpeed) + camera.dirY * cos(-Camera::rotSpeed);*/

			camera.yrot += Camera::rotSpeed;
		}

		if (keys[SDLK_SPACE])
		{
			puts("test");
		}

		camera.orient();

		/*camera.orient();
		glPushMatrix();
		glTranslatef(0.0f,0.0f,-60.0f);
		glBindTexture(GL_TEXTURE_2D, gun);
		glColor3f(1.0f, 1.0f, 1.0f);
		glBegin(GL_QUADS);
			glTexCoord2i(0, 0);
			glVertex3f(-1.0f, 1.0f, 0.0f);
			glTexCoord2i(1, 0);
			glVertex3f(1.0f, 1.0f, 0.0f);
			glTexCoord2i(1, 1);
			glVertex3f( 1.0f,-1.0f, 0.0f);
			glTexCoord2i(0, 1);
			glVertex3f(-1.0f,-1.0f, 0.0f);
		glEnd();
		glPopMatrix();*/

		deltaTime = SDL_GetTicks() - lastTime;

		if (deltaTime >= 1000 / 60)
		{
			glBindTexture(GL_TEXTURE_2D, wall);
			for (int i = 0; i < MAP_HEIGHT; i++)
			{
				for (int j = 0; j < MAP_WIDTH; j++)
				{
					if (worldMap[i][j] != 0)
					{
						glPushMatrix();
						//glColor3f(1.0f, 0.0f, 0.0f);
						glTranslatef((float) i* 2.0f, 0.0f, (float) j * 2.0f);
						glCallList(displayLists[BLOCK]);
						glPopMatrix();

						glColor3f(1.0f, 1.0f, 1.0f);
					}
				}
			}

			glPushMatrix();
			switchTo2D();
			glTranslatef(0.0f, -6.0f, 0.0f);
			glBindTexture(GL_TEXTURE_2D, gun);
			glBegin(GL_QUADS);
			glTexCoord2i(0, 0);
			glVertex2f(-10.0f, 20.0f);
			glTexCoord2i(1, 0);
			glVertex2f(10.0f, 20.0f);
			glTexCoord2i(1, 1);
			glVertex2f(10.0f, -20.0f);
			glTexCoord2i(0, 1);
			glVertex2f(-10.0f, -20.0f);
			glEnd();
			glPopMatrix();
			switchTo3D();


			glFlush();
			SDL_GL_SwapBuffers();
		}


	}

	SDL_Quit();

	return 0;
}

